const mysql = require('../../utils/db');
const BaseModel = require('../BaseModel');
const _table_name = "music";
const order = " ORDER BY music_id DESC";

exports.findAll = async (success, fail) => {
    let sqlStr = `
        SELECT
            m.music_id,
            music_name,
            m.singer_id,
            IFNULL(singer_name, '未知歌手') AS singer_name,
            IFNULL(singer_sex, 1) AS singer_sex,
            m.lyric_author_id,
            IFNULL(
                lyric_author_name,
                '未知词作者'
            ) AS lyric_author_name,
            m.melody_author_id,
            IFNULL(
                melody_author_name,
                '未知曲作者'
            ) AS melody_author_name,
            m.album_id,
            IFNULL(album_name, '未知专辑') AS album_name,
            music_issue_time,
            music_cover_name,
            music_cover_src,
            music_file_name,
            music_file_src,
            ml.lyric_id,
            ml.lyric_src,
            ml.lyric_file_name,
            is_lock
        FROM
            ${_table_name} m
        LEFT JOIN singer s ON (m.singer_id = s.singer_id)
        LEFT JOIN lyric_author la ON (m.lyric_author_id= la.lyric_author_id)
        LEFT JOIN melody_author ma ON (m.melody_author_id= ma.melody_author_id)
        LEFT JOIN music_album al ON (m.album_id= al.album_id)
        LEFT JOIN music_lyric ml ON (m.lyric_id= ml.lyric_id)
        ${order}
        `

    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.updateMusic = async (params, updatekey, success, fail) => {
    let sqlStr = BaseModel.update(_table_name, params, updatekey);

    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.findMusicByids = async (ids, is_lock = null) => {
    this.data = null;
    this.err = null;
    let sqlStr = `
    SELECT
	m.*, s.singer_name,
	l.lyric_author_name,
	ml.melody_author_name,
	al.album_name
    FROM
        ${_table_name} m
    LEFT JOIN singer s ON s.singer_id = m.singer_id
    LEFT JOIN lyric_author l ON l.lyric_author_id = m.lyric_author_id
    LEFT JOIN melody_author ml ON ml.melody_author_id = m.melody_author_id
    LEFT JOIN music_album al ON al.album_id = m.album_id
    WHERE
        m.music_id IN (
            ${ids}
        )
    `;

    if (is_lock)
        sqlStr += ` and is_lock='${is_lock}'`

    await mysql.row(sqlStr)
        .then(data => {
            this.data = data;
        })
        .catch(err => {
            this.err = err;
        });

    this.success = (fn) => {
        if (this.data)
            return fn(this.data);
    }
    this.fail = (fn) => {
        if (this.err)
            return fn(this.err);
    }
    return this;
}


exports.findRand = (params, success, fail) => {
    let {
        limit
    } = params;
    let order = " ORDER BY RAND() limit " + limit;
    let sqlStr = `
        SELECT
        m.*, s.singer_name,
        al.album_name
        FROM
            ${_table_name} m
        LEFT JOIN singer s ON (m.singer_id = s.singer_id)
        LEFT JOIN music_album al ON (m.album_id = al.album_id)
       
        ${order}`;
    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.findMusicByName = async (name) => {
    this.data = null;
    this.err = null;
    let sqlStr = `
            SELECT
            m.*, s.singer_name,
            al.album_name
            FROM
                ${_table_name} m
            LEFT JOIN singer s ON (m.singer_id = s.singer_id)
            LEFT JOIN music_album al ON (m.album_id = al.album_id)
            WHERE
                m.music_name LIKE '%${name}%'
            OR s.singer_name LIKE '%${name}%'
            OR al.album_name LIKE '%${name}%'
        `

    await mysql.row(sqlStr)
        .then(data => {
            this.data = data;
        })
        .catch(err => {
            this.err = err;
        });

    this.success = (fn) => {
        if (this.data)
            return fn(this.data);
    }
    this.fail = (fn) => {
        if (this.err)
            return fn(this.err);
    }
    return this;
}

exports.findMusic = async (params) => {
    this.data = null;
    this.err = null;

    let sqlStr = `
        SELECT
            m.music_id,
            music_name,
            m.singer_id,
            IFNULL(singer_name, '未知歌手') AS singer_name,
            IFNULL(singer_sex, 1) AS singer_sex,
            m.lyric_author_id,
            IFNULL(
                lyric_author_name,
                '未知词作者'
            ) AS lyric_author_name,
            m.melody_author_id,
            IFNULL(
                melody_author_name,
                '未知曲作者'
            ) AS melody_author_name,
            m.album_id,
            IFNULL(album_name, '未知专辑') AS album_name,
            music_issue_time,
            music_cover_name,
            music_cover_src,
            music_file_name,
            music_file_src,
            ml.lyric_id,
            ml.lyric_src,
            ml.lyric_file_name,
            is_lock
        FROM
            ${_table_name} m
        LEFT JOIN singer s ON (m.singer_id = s.singer_id)
        LEFT JOIN lyric_author la ON (m.lyric_author_id= la.lyric_author_id)
        LEFT JOIN melody_author ma ON (m.melody_author_id= ma.melody_author_id)
        LEFT JOIN music_album al ON (m.album_id= al.album_id)
        LEFT JOIN music_lyric ml ON (m.lyric_id= ml.lyric_id)
        where 
        `
    sqlStr = BaseModel.findCondition(sqlStr, params);
    sqlStr += ` ${order}`;
    await mysql.row(sqlStr)
        .then(data => {
            this.data = data;
        })
        .catch(err => {
            this.err = err;
        });

    this.success = (fn) => {
        if (this.data)
            return fn(this.data);
    }
    this.fail = (fn) => {
        if (this.err)
            return fn(this.err);
    }
    return this;
}

exports.delMusic = (params, success, fail) => {
    let sqlStr = BaseModel.delete(_table_name, params);
    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.addMusic = (params, success, fail) => {
    let sqlStr = BaseModel.add(_table_name, params);

    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}